package cn.iutils.faq.taglib;

import cn.iutils.common.Page;
import cn.iutils.common.spring.SpringUtils;
import cn.iutils.faq.entity.Questions;
import cn.iutils.faq.service.AnswersService;
import cn.iutils.faq.service.QuestionsService;

import java.util.List;
import java.util.Map;

/**
 * 问答系统 库方法
 * @author iutils.cn
 */
public class FaqLib {

    /**
     * 回答服务
     */
    private static AnswersService answersService = SpringUtils.getBean(AnswersService.class);

    /**
     * 问题服务
     */
    private static QuestionsService questionsService = SpringUtils.getBean(QuestionsService.class);

    /**
     * 返回问题信息
     * @param id
     * @return
     */
    public static Questions getQuestion(String id){
        questionsService.plusViews(id);//问题浏览数+1
        return questionsService.get(id);
    }


    /**
     * 获取分页问题列表
     * @param pageNo
     * @param pageSize
     * @param orderBy
     * @return
     */
    public static Page<Questions> getFaqPage(String key,int pageNo,int pageSize,String orderBy){
        Page<Questions> page = new Page<Questions>();
        page.setPageNo(pageNo < 0 ? 0 : pageNo);
        page.setPageSize(pageSize < 10 ? 10 : pageSize);
        page.setOrderBy(orderBy);
        try{
            key = new String(key.getBytes("iso8859-1"),"utf-8");
        }catch (Exception e){}
        page.setKey(key);
        Questions questions = new Questions();
        page.setEntity(questions);
        return page.setList(questionsService.findPage(page));
    }

    /**
     * 获取活跃用top
     * @param size
     * @return
     */
    public static List<Map> getAnswersUserTop(int size){
        return answersService.findHotList(size);
    }

    /**
     * 获取热门问题top
     * @param size
     * @return
     */
    public static List<Questions> getQuestionsUserTop(int size){
        return null;
    }

}
